package com.ruoyi.multab.controller;

import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.multab.domain.MulModelParam;
import com.ruoyi.multab.service.IMulModelCalcService;
import com.ruoyi.multab.service.IMulModelMainService;
import com.ruoyi.multab.service.IMulModelParamService;
import com.ruoyi.multab.utils.MulExcelObj;
import com.ruoyi.multab.utils.MulExcelUtils;
import com.ruoyi.multab.websocket.WebSocketServer;

/**
 * 模型接口Controller
 * 
 * @author zhouwc
 * @date 2020-08-08
 */
@Controller
@RequestMapping("/multab/multabapi")
public class MulModelApiController extends BaseController {
	
    private static final Logger log = LoggerFactory.getLogger(MulModelApiController.class);
	
	private String prefix = "multab/multabemodel";

	@Autowired
	private IMulModelMainService mulModelMainService;
	@Autowired
	private IMulModelParamService mulModelParamService;
	@Autowired
    private WebSocketServer webSocketServer;
	@Autowired
    @Qualifier("calcServiceImpl1")
	private IMulModelCalcService mulModelCalcService;

	@RequiresPermissions("multab:multabmain:view")
	@GetMapping("/multab1")
	public String multab1(ModelMap mmap) {
		MulModelParam mulModelParam = new MulModelParam();
		mulModelParam.setMainId("1");
		List<MulModelParam> paramList = mulModelParamService.selectMulModelParamList(mulModelParam);
		mmap.put("paramList", paramList);

		String sessionId = ServletUtils.getSession().getId();
		mmap.put("sessionId", sessionId);

		return prefix + "/multab1";
	}

	@PostMapping("/multab1/upload")
	@ResponseBody
	public AjaxResult uploadFile(MultipartFile file) throws Exception {
		try {
			HttpSession session = ServletUtils.getSession();
			String sessionId = session.getId();
			MulExcelObj readExcel = MulExcelUtils.readExcel(file.getInputStream());
			session.setAttribute(sessionId, readExcel);
			AjaxResult ajax = AjaxResult.success();
			ajax.put("data", readExcel);
			return ajax;
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}
	//开始执行
	@PostMapping("/multab1/run1")
	@ResponseBody
	public AjaxResult run1(@RequestBody Map<String,Object> map) throws Exception {
		try {
			HttpSession session = ServletUtils.getSession();
			String sessionId = session.getId();
			MulExcelObj readExcel = (MulExcelObj) session.getAttribute(sessionId);
			List<JSONObject> titleArray = readExcel.getTitleArray();
			List<JSONObject> dataArray = readExcel.getDataArray();
			int totalCellNum = readExcel.getTotalCellNum();
			
			log.info("run1.readExcel={}",readExcel);
			log.info("run1.titleArray={}",titleArray);
			log.info("run1.dataArray={}",dataArray);
			
			Double[][] inputValues = transDataArrayToDoubleArray(dataArray,totalCellNum);
			log.debug("inputValues={}",JSONObject.toJSON(inputValues));
			
			//{Pc=0.8, Pm=0.2, M=10, DaiNo=2, Ci=20, ads=1, ac=0.000001, exV=5, par_Q=1, coe_Q=0.1, Run_No=10, Max_max_No=1,2,3,4,5,6,7,8,9,10,11,12}
			Map<String,String> runParam = (Map<String, String>) map.get("paramData");
			//[{searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={},
			//id=null, mainId=null, param1=8, param2=6.11, param3=6, param4=3, param5=0.02, param6=6.44,
			//param7=6, param8=6.67, param9=0.17, param10=6, param11=1, param12=7.78}, 
			//{searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=5.56, param2=5.89, param3=6.67, param4=3, param5=0.02, param6=7.44, param7=6.33, param8=7.33, param9=0.85, param10=7.33, param11=0.61, param12=7.56}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.33, param2=6.56, param3=6, param4=7, param5=0.05, param6=6.89, param7=7, param8=7.78, param9=0.59, param10=6.67, param11=0.89, param12=7.78}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=4, param2=5.78, param3=5.44, param4=2, param5=0.02, param6=7, param7=5.89, param8=6.78, param9=0.46, param10=6.22, param11=0.83, param12=8.11}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.89, param2=6, param3=7.11, param4=3, param5=0.04, param6=6.89, param7=6, param8=7, param9=0.83, param10=7, param11=0.80, param12=7.11}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=5.89, param2=7.33, param3=6.78, param4=3, param5=0.02, param6=7, param7=6.67, param8=6.56, param9=0.94, param10=7.33, param11=0.83, param12=6.89}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.89, param2=7.33, param3=7.78, param4=5, param5=0.03, param6=7.56, param7=7.33, param8=8, param9=0.68, param10=7.56, param11=0.78, param12=7.78}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=6.22, param2=6.44, param3=6.78, param4=2, param5=0.02, param6=7.56, param7=7.22, param8=7.56, param9=0.63, param10=7.78, param11=0.80, param12=7.67}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=5.78, param2=8, param3=7.33, param4=4, param5=0.04, param6=7.89, param7=7.67, param8=8, param9=0.93, param10=7.78, param11=0.73, param12=8.11}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.11, param2=7.56, param3=7.67, param4=6, param5=0.04, param6=7.89, param7=7.56, param8=7.78, param9=0.96, param10=7.89, param11=0.77, param12=8.56}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=8.22, param2=6.89, param3=6.33, param4=3, param5=0.02, param6=8, param7=6.22, param8=7.67, param9=0.95, param10=8, param11=0.77, param12=8.44}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=6.44, param2=7, param3=7.56, param4=4, param5=0.03, param6=7.78, param7=8.11, param8=7.67, param9=1.44, param10=7.89, param11=0.72, param12=8.22}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=8.33, param2=5.67, param3=5.44, param4=4, param5=0.03, param6=6.11, param7=5.78, param8=6.78, param9=0.70, param10=6.22, param11=0.70, param12=7.67}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.89, param2=6.67, param3=6.67, param4=3, param5=0.01, param6=7.67, param7=7, param8=7.67, param9=0.70, param10=7.56, param11=0.67, param12=7.89}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=5, param2=5.89, param3=5.89, param4=3, param5=0.01, param6=6.78, param7=6.33, param8=6.89, param9=0.75, param10=6.33, param11=0.92, param12=7.44}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.44, param2=6.11, param3=6.11, param4=4, param5=0.03, param6=7, param7=6.89, param8=6.89, param9=0.59, param10=6.67, param11=1, param12=7.67}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=8.67, param2=6.33, param3=5.78, param4=5, param5=0.04, param6=5.89, param7=6.11, param8=6.11, param9=0.58, param10=6.33, param11=0.75, param12=7.44}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.89, param2=6.11, param3=6, param4=5, param5=0.03, param6=7, param7=6.22, param8=7.11, param9=0.74, param10=6.56, param11=0.90, param12=7.78}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.56, param2=6.56, param3=6.67, param4=4, param5=0.03, param6=7.11, param7=6.89, param8=7.22, param9=0.65, param10=7.33, param11=0.82, param12=7.78}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=8.44, param2=5.89, param3=6.56, param4=6, param5=0.02, param6=7.56, param7=6.56, param8=8.33, param9=0.48, param10=8, param11=0.82, param12=8}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.67, param2=7.67, param3=7.89, param4=7, param5=0.05, param6=8.11, param7=8.22, param8=7.89, param9=1.28, param10=8.33, param11=0.67, param12=8.22}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=6.22, param2=6.56, param3=7.22, param4=3, param5=0.01, param6=6.89, param7=7.67, param8=6.89, param9=0.97, param10=7.56, param11=0.71, param12=7.78}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=8.33, param2=6.44, param3=7.89, param4=5, param5=0.02, param6=6.67, param7=7.56, param8=6.78, param9=0.42, param10=7, param11=0.86, param12=7.67}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.89, param2=6.11, param3=6.44, param4=3, param5=0.02, param6=7.33, param7=6.67, param8=7.44, param9=0.35, param10=7, param11=0.86, param12=7.78}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=8.22, param2=5.33, param3=6, param4=5, param5=0.01, param6=7.22, param7=7, param8=6.89, param9=0.56, param10=7, param11=0.80, param12=7.44}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=7.89, param2=6.67, param3=5.67, param4=2, param5=0.04, param6=6, param7=6.44, param8=5.78, param9=0.34, param10=6.11, param11=1, param12=7.44}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=6.67, param2=8, param3=7, param4=4, param5=0.03, param6=7.56, param7=7.67, param8=7.56, param9=0.53, param10=7.22, param11=0.62, param12=8.11}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=6.78, param2=5.67, param3=6.78, param4=2, param5=0.02, param6=7, param7=6.56, param8=7.11, param9=0.66, param10=7.11, param11=0.73, param12=7.67}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=5.22, param2=6.89, param3=5.56, param4=1, param5=0.02, param6=8.22, param7=5.78, param8=8.56, param9=1.13, param10=8.33, param11=0.87, param12=8.22}, {searchValue=null, createBy=null, createTime=null, updateBy=null, updateTime=null, remark=null, params={}, id=null, mainId=null, param1=5.33, param2=6.78, param3=5.67, param4=2, param5=0.01, param6=8.11, param7=5.56, param8=8.56, param9=1.21, param10=8.22, param11=0.59, param12=8}]
			List<Map<String,String>> runDataArray = (List<Map<String, String>>) map.get("tableData");
			
//			Double[][] inputValues = transListToDoubleArray(runDataArray);
			
			new Thread(new Runnable() {
				@Override
				public void run() {
					mulModelCalcService.execCalc(sessionId, runParam, inputValues);
				}
			}).start();
			
			
			AjaxResult ajax = AjaxResult.success("数据校验通过,开始执行,预计2秒后开始刷新数据!");
			return ajax;
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}
	private Double[][] transDataArrayToDoubleArray(List<JSONObject> dataArray,int totalCellNum) {
		Double[][] inputValues = new Double[dataArray.size()][];
		
		for(int i = 0;i<dataArray.size();i++) {
			JSONObject json = dataArray.get(i);
			
			inputValues[i] = new Double[totalCellNum];
			for(int j = 0;j<totalCellNum;j++) {
				inputValues[i][j] = Double.parseDouble(json.getString("dataValue"+j));
			}
		}
		
		return inputValues;
	}
	private Double[][] transListToDoubleArray(List<Map<String, String>> runDataArray) {
		Double[][] inputValues = new Double[runDataArray.size()][];
		
		for(int i = 0;i<runDataArray.size();i++) {
			Map<String, String> map = runDataArray.get(i);
			
			inputValues[i] = new Double[12];
			
			inputValues[i][0] = Double.parseDouble(map.get("param1"));
			inputValues[i][1] = Double.parseDouble(map.get("param2"));
			inputValues[i][2] = Double.parseDouble(map.get("param3"));
			inputValues[i][3] = Double.parseDouble(map.get("param4"));
			inputValues[i][4] = Double.parseDouble(map.get("param5"));
			inputValues[i][5] = Double.parseDouble(map.get("param6"));
			inputValues[i][6] = Double.parseDouble(map.get("param7"));
			inputValues[i][7] = Double.parseDouble(map.get("param8"));
			inputValues[i][8] = Double.parseDouble(map.get("param9"));
			inputValues[i][9] = Double.parseDouble(map.get("param10"));
			inputValues[i][10] = Double.parseDouble(map.get("param11"));
			inputValues[i][11] = Double.parseDouble(map.get("param12"));
			
		}
		
		return inputValues;
	}

	//测试websocket
	@GetMapping("/multab1/sendMsg")
	@ResponseBody
	public AjaxResult sendMsg() throws Exception {
		try {
			String sessionId = ServletUtils.getSession().getId();
			for(int x = 1;x<1000;x++) {
				int y = new Random().nextInt(1000);
				
				webSocketServer.sendInfo(sessionId, x+"-"+y);
				Thread.sleep(500);
			}
			AjaxResult ajax = AjaxResult.success(sessionId);
			return ajax;
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}

}
